#include "preset_control/preset_control.hpp"
#ifdef DBG_TAG
#undef DBG_TAG
#endif
#ifdef DBG_LVL
#undef DBG_LVL
#endif

#define DBG_TAG "preset_def"
#define DBG_LVL DBG_LOG

extern "C"
{
#include <rtdbg.h>
}
/******************************************************************************
 *
 * @brief:    系统状态机
 *
 ******************************************************************************/
STATE* system_mechine;

/******************************************************************************
 *
 * @brief:    正常工作指示灯
 *
 ******************************************************************************/

BEEP_LED* sys_led;

/******************************************************************************
 *
 * @brief:    数据储存
 *
 ******************************************************************************/

EEPROM *eeprom;
DATA_SAVE *global_data;


/******************************************************************************
 *
 * @brief:    电参数测量
 *
 ******************************************************************************/


double transfer_adc_value_to_need_V(uint32_t adc_value)
{
    double pin_V; //adc管脚电压
    double result; //实际输入电压

    pin_V = adc_value * 3.3 / 4096; //adc管脚电压换算
    result = pin_V*5.2531 + 0.0043;

    return result;
}

CHIP_ADC *voltage_device;

/******************************************************************************
 *
 * @brief:    光流传感器预定义
 *
 ******************************************************************************/

CHIP_SPI* flow_spi;
CHIP_INPUT_PIN* flow_motion;
CHIP_OUTPUT_PIN* flow_reset;

PMW3901* flow_device;

/******************************************************************************
 *
 * @brief:    风扇预定义
 *
 ******************************************************************************/

CHIP_PWM* fan_pwm;


/******************************************************************************
 *
 * @brief:    补光板预定义
 *
 ******************************************************************************/
CHIP_PWM* led_board_pwm;

/******************************************************************************
 *
 * @brief:    超声波预定义
 *
 ******************************************************************************/
CHIP_OUTPUT_PIN *ultra_sonic_en;
CHIP_UART* ultra_sonic_uart;
DISTANCE_SONIC *ultra_sonic;

/******************************************************************************
 *
 * @brief:    模拟轮速预定义
 *
 ******************************************************************************/
CHIP_OUTPUT_PIN *A;
CHIP_OUTPUT_PIN *B;
SIMULATE_SIGNAL *sim_sig;

/******************************************************************************
 *
 * @brief:    预定义实例化
 *
 ******************************************************************************/
preset_status preset_create_obj()
{
    struct serial_configure serial_config;

    /******************************************************************************
     *
     * @brief:    系统状态机
     *
     ******************************************************************************/
    system_mechine = new STATE("sys");
     if(!system_mechine)
     {
        LOG_E("STATE<sys> create obj fail");
        goto end;
     }
     /******************************************************************************
      *
      * @brief:    正常工作指示灯
      *
      ******************************************************************************/

     sys_led = new BEEP_LED (GET_PIN(A,0),beep_led_active_LOW,beep_led_working_mode_continue);
     if(!sys_led)
     {
        LOG_E("BEEP_LED<A,0> create obj fail");
        goto end;
     }
    /******************************************************************************
     *
     * @brief:    数据储存
     *
     ******************************************************************************/

    eeprom =new EEPROM(GET_PIN(B, 7),
                    "i2c1",
                    128,
                    64 * 1024,
                    EEPROM_identity_code_BL24C512A_normal,
                    EEPROM_A2A1A0_mode_0_0_0,
                    EEPROM_word_addr_bit_16);
    if(!eeprom)
    {
        LOG_E("EEPROM create obj fail");
        goto end;
    }


    global_data =new DATA_SAVE(eeprom,0,1,1,5,6,100);
    if(!global_data)
    {
        LOG_E("DATA_SAVE create obj fail");
        goto end;
    }


    /******************************************************************************
     *
     * @brief:    电参数测量
     *
     ******************************************************************************/

    voltage_device =new CHIP_ADC ("adc1", 1, 100, FA_Filter_Type_AA, transfer_adc_value_to_need_V);
    if(!voltage_device)
    {
        LOG_E("CHIP_ADC create obj fail");
        goto end;
    }
    /******************************************************************************
     *
     * @brief:    光流传感器
     *
     ******************************************************************************/

    flow_spi =new CHIP_SPI ("spi1", "pmw3901", GPIO_PIN_4, GPIOA, 8, 1*1000*1000, (RT_SPI_MASTER | RT_SPI_MODE_3 | RT_SPI_MSB));
    if(!flow_spi)
    {
        LOG_E("CHIP_SPI create obj fail");
        goto end;
    }

    flow_motion =new CHIP_INPUT_PIN (GET_PIN(C,5), chip_input_pin_mode_simple, chip_input_pin_pull_mode_none, NULL, NULL);
    if(!flow_motion)
    {
        LOG_E("CHIP_INPUT_PIN<flow_motion> create obj fail");
        goto end;
    }

    flow_reset =new CHIP_OUTPUT_PIN (GET_PIN(C,4), chip_output_pin_out_mode_normal, chip_pin_level_HIGH);
    if(!flow_reset)
    {
        LOG_E("CHIP_OUTPUT_PIN<flow_reset> create obj fail");
        goto end;
    }


    flow_device =new PMW3901 (flow_spi, flow_reset, flow_motion,1);
    if(!flow_device)
    {
        LOG_E("PMW3901 create obj fail");
        goto end;
    }

    /******************************************************************************
     *
     * @brief:    风扇
     *
     ******************************************************************************/


    fan_pwm = new CHIP_PWM ("pwm12", 2, chip_pwm_drive_type_HIGH, 10000, 99);
    if(!fan_pwm)
    {
        LOG_E("CHIP_PWM<fan_pwm> create obj fail");
        goto end;
    }

    /******************************************************************************
     *
     * @brief:    补光板
     *
     ******************************************************************************/
    led_board_pwm = new CHIP_PWM ("pwm12", 1, chip_pwm_drive_type_HIGH, 10000, 99);
    if(!led_board_pwm)
    {
        LOG_E("CHIP_PWM<led_board_pwm> create obj fail");
        goto end;
    }

    /******************************************************************************
     *
     * @brief:    超声波
     *
     ******************************************************************************/
//#define RT_SERIAL_CONFIG_DEFAULT           \
//{                                          \
//    BAUD_RATE_115200, /* 115200 bits/s */  \
//    DATA_BITS_8,      /* 8 databits */     \
//    STOP_BITS_1,      /* 1 stopbit */      \
//    PARITY_NONE,      /* No parity  */     \
//    BIT_ORDER_LSB,    /* LSB first sent */ \
//    NRZ_NORMAL,       /* Normal mode */    \
//    RT_SERIAL_RB_BUFSZ, /* Buffer size */  \
//    RT_SERIAL_FLOWCONTROL_NONE, /* Off flowcontrol */ \
//    0                                      \
//}
//    struct serial_configure
//    {
//        rt_uint32_t baud_rate;
//
//        rt_uint32_t data_bits               :4;
//        rt_uint32_t stop_bits               :2;
//        rt_uint32_t parity                  :2;
//        rt_uint32_t bit_order               :1;
//        rt_uint32_t invert                  :1;
//        rt_uint32_t bufsz                   :16;
//        rt_uint32_t flowcontrol             :1;
//        rt_uint32_t reserved                :5;
//    };

    serial_config.baud_rate = BAUD_RATE_115200;
    serial_config.data_bits = DATA_BITS_8;
    serial_config.stop_bits = STOP_BITS_1;
    serial_config.parity = PARITY_NONE;
    serial_config.bit_order = BIT_ORDER_LSB;
    serial_config.invert = NRZ_NORMAL;
    serial_config.bufsz = 256;
    serial_config.flowcontrol = RT_SERIAL_FLOWCONTROL_NONE;
    serial_config.reserved = 0;

    ultra_sonic_en = new CHIP_OUTPUT_PIN(GET_PIN(C,8),chip_output_pin_out_mode_normal,chip_pin_level_LOW);
    if(!ultra_sonic_en)
    {
        LOG_E("CHIP_OUTPUT_PIN<ultra_sonic_en> create obj fail");
        goto end;
    }

    ultra_sonic_uart = new CHIP_UART("uart6",&serial_config);
    if(!ultra_sonic_uart)
    {
        LOG_E("CHIP_UART create obj fail");
        goto end;
    }

    ultra_sonic = new DISTANCE_SONIC(ultra_sonic_uart,ultra_sonic_en);
    if(!ultra_sonic)
    {
        LOG_E("DISTANCE_SONIC create obj fail");
        goto end;
    }


    /******************************************************************************
     *
     * @brief:    模拟轮速预定义
     *
     ******************************************************************************/
    A = new CHIP_OUTPUT_PIN (GET_PIN(B,9), chip_output_pin_out_mode_normal, chip_pin_level_LOW);
    if(!A)
    {
        LOG_E("CHIP_OUTPUT_PIN<A> create obj fail");
        goto end;
    }

    B = new CHIP_OUTPUT_PIN (GET_PIN(B,8), chip_output_pin_out_mode_normal, chip_pin_level_LOW);
    if(!B)
    {
        LOG_E("CHIP_OUTPUT_PIN<B> create obj fail");
        goto end;
    }

    sim_sig = new SIMULATE_SIGNAL(A,B,50);
    if(!sim_sig)
    {
        LOG_E("SIMULATE_SIGNAL create obj fail");
        goto end;
    }

    LOG_I("create obj ok");
    return preset_status_ok;
end:
    LOG_E("create obj fail");
    return preset_status_fail;


}
